// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Mvvm
{
	public class ItemWrapper : ContentControl
	{
		static ItemWrapper()
		{
			DefaultStyleKeyProperty.OverrideMetadata(
				typeof(ItemWrapper),
				new FrameworkPropertyMetadata(typeof(ItemWrapper)));
		}

		public string ViewFactoryKey { get; set; }

		public ItemWrapper()
		{
			Loaded += ItemsControlContent_Loaded;
		}

		#region VisualDelay DP

		public static readonly DependencyProperty VisualDelayProperty =
			DependencyProperty.Register(
				"VisualDelay", typeof(TimeSpan), typeof(ItemWrapper),
				new PropertyMetadata(default(TimeSpan)));

		public TimeSpan VisualDelay
		{
			get { return (TimeSpan)GetValue(VisualDelayProperty); }
			set { SetValue(VisualDelayProperty, value); }
		}

		#endregion

		void ItemsControlContent_Loaded(object sender, RoutedEventArgs e)
		{
			Loaded -= ItemsControlContent_Loaded;

			var view = DynamicDataTemplate.ViewFactories[ViewFactoryKey].GetViewForModel(DataContext);
			Content = view;

			Animate();
		}

		private void Animate()
		{
			Opacity = 1;
			return;

			var storyboard = new Storyboard();
			var opacityAnimation = new DoubleAnimation(
				1,
				new Duration(TimeSpan.FromMilliseconds(500)))
				{
					BeginTime = VisualDelay
				};

			Storyboard.SetTarget(opacityAnimation, this);
			Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));

			storyboard.Children.Add(opacityAnimation);
			storyboard.Begin();
		}
	}
}